import {NAMESPACE_USER} from "../interface/user.ts";
import useAxiosInstance from "../util/config.ts";
import {CommonModel} from "../interface/commonModel.ts";
import {getCookieValue} from "../store/cookieStore.ts";

export const loginStatus = ():boolean=>{
    return !!getCookieValue("token");
}

export const userLogin = async (options:NAMESPACE_USER.UserLogin):Promise<CommonModel.ApiResponse>=>{
    let res = await useAxiosInstance.post("/user/login",options)
    return res.data
}

export const getCurrentUser = async ():Promise<CommonModel.ApiResponse>=>{
    let res = await useAxiosInstance.get("/user/current")
    return res.data
}

export const queryUser = async (options:NAMESPACE_USER.UserQuery):Promise<CommonModel.ApiResponse>=>{
    let res = await useAxiosInstance<NAMESPACE_USER.UserItem,any>({
        url:"/user/list",
        params:options,
    });
    return res.data
}

export const updateUser = async (options:NAMESPACE_USER.UserItem):Promise<CommonModel.ApiResponse>=>{
    let res = await useAxiosInstance.post("/user/update",options)
    return res.data
}


export const addUser = async (options:NAMESPACE_USER.UserBase):Promise<CommonModel.ApiResponse>=>{
    let res = await useAxiosInstance.post("/user/register",options)
    return res.data
}


export const delUser = async (ids:Array<number|string>):Promise<CommonModel.ApiResponse>=>{
    let res = await useAxiosInstance.delete("/user/delete",{
        headers: {
            'Content-Type': 'application/json'
        },
        data : {ids:ids}
    });
    return res.data
}


// 用户组api

export const queryUserGroup = async (options:NAMESPACE_USER.UserGroupQuery):Promise<CommonModel.ApiResponse>=>{
    let res = await useAxiosInstance<NAMESPACE_USER.UserGroupItem,any>({
        url:"/user/group/list",
        params:options,
    });
    return res.data
}

export const createUserGroup = async (options:NAMESPACE_USER.UserGroupBase):Promise<CommonModel.ApiResponse>=>{
    let res = await useAxiosInstance.post("/user/group/create",options)
    return res.data
}

export const updateUserGroup = async (options:NAMESPACE_USER.UserGroupUpdate):Promise<CommonModel.ApiResponse>=>{
    let res = await useAxiosInstance.post("/user/group/update",options)
    return res.data
}


export const deleteUserGroup = async (ids:Array<number|string>):Promise<CommonModel.ApiResponse>=>{
    let res = await useAxiosInstance.post("/user/group/delete",{ids:ids});
    return res.data
}


export const userAddToGroup = async (options:NAMESPACE_USER.UserAddGroup):Promise<CommonModel.ApiResponse>=>{
    let res = await useAxiosInstance.post("/user/add/group",options)
    return res.data
}


//角色api
export const queryRole = async (options:NAMESPACE_USER.RoleQuery):Promise<CommonModel.ApiResponse>=>{
    let res = await useAxiosInstance<NAMESPACE_USER.RoleItem,any>({
        url:"/user/role/list",
        params:options,
    });
    return res.data
}

export const createRole = async (options:NAMESPACE_USER.RoleBase):Promise<CommonModel.ApiResponse>=>{
    let res = await useAxiosInstance.post("/user/role/create",options)
    return res.data
}

export const updateRole = async (options:NAMESPACE_USER.RoleItem):Promise<CommonModel.ApiResponse>=>{
    let res = await useAxiosInstance.post("/user/role/update",options)
    return res.data
}


export const deleteRole = async (ids:Array<number|string>):Promise<CommonModel.ApiResponse>=>{
    let res = await useAxiosInstance.post("/user/role/delete",{ids:ids});
    return res.data
}

//权限api
export const queryPermission = async (options:NAMESPACE_USER.PermissionQuery):Promise<CommonModel.ApiResponse>=>{
    let res = await useAxiosInstance<NAMESPACE_USER.PermissionItem,any>({
        url:"/user/permission/list",
        params:options,
    });
    return res.data
}

export const createPermission= async (options:NAMESPACE_USER.PermissionBase):Promise<CommonModel.ApiResponse>=>{
    let res = await useAxiosInstance.post("/user/permission/create",options)
    return res.data
}

export const updatePermission = async (options:NAMESPACE_USER.PermissionItem):Promise<CommonModel.ApiResponse>=>{
    let res = await useAxiosInstance.post("/user/permission/update",options)
    return res.data
}


export const deletePermission = async (ids:Array<number|string>):Promise<CommonModel.ApiResponse>=>{
    let res = await useAxiosInstance.post("/user/permission/delete",{ids:ids});
    return res.data
}